package com.read.villager.servlet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.read.utils.ModelUtils;
import com.read.utils.StringUtils;
import com.read.villager.entity.vo.VillagerInfoVO;

/**
 * 民族信息servlet
 * @author ksir
 *
 */
@MultipartConfig
@WebServlet(urlPatterns={"/nation/upload","/nation/import"})
public class NationsServlet extends HttpServlet{
	
	private static final Logger log = LoggerFactory.getLogger(NationsServlet.class);
	private static final long serialVersionUID = 6744459123704204721L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//log.debug("");
		Map<String,String[]> map = req.getParameterMap();
		/*for(String str:map.keySet()){
			log.debug("key :{},value:{}",str,StringUtils.toString(map.get(str)));
		}*/
		
		ModelUtils.MapToModel(map, VillagerInfoVO.class);
		VillagerInfoVO obj = (VillagerInfoVO) ModelUtils.MapToModel(map, VillagerInfoVO.class);
		/*Enumeration<String > e = req.getParameterNames();
		while(e.hasMoreElements()){
			log.debug(e.nextElement());
		}*/
		/*String uri = req.getRequestURI();
		log.debug(uri);
		if(uri.endsWith("upload")){
			upload(req,resp);
		}
		if(uri.endsWith("import")){
			importFile(resp);
		}*/
	}
	
	private void importFile( HttpServletResponse resp) throws IOException{
		
		//resp.sendRedirect("/import.jsp");
	}
	
	private void upload(HttpServletRequest req,HttpServletResponse resp) throws IllegalStateException, IOException, ServletException{
		Part part = req.getPart("nationFile");
		String header = part.getHeader("Content-Disposition");
		log.debug(header);
		String fileName = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\""));
		log.debug(fileName);
		writeTo(fileName, part);
		resp.sendRedirect("/index.jsp");
	}
	
	private void writeTo(String fileName, Part part) throws IOException, FileNotFoundException {
		 File file = new File("upload/nation/");
		 if(!file.exists()){
			 file.mkdirs();
		 }
        InputStream in = part.getInputStream();
        OutputStream out = new FileOutputStream("upload/nation/"+fileName);
        byte[] buffer = new byte[1024];
        int length = -1;
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);
        }

        in.close();
        out.close();
    }
}
